﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AlarmManagePlatform.UC.ComboBox
{
    public partial class SubmitUnit : UserControl
    {
        List<Model.DB.tb_SubmitUnit> _ListAll, _ListSearch;
        public SubmitUnit()
        {
            InitializeComponent();
            
            cmbSumitUnit.DropDownStyle = ComboBoxStyle.DropDown;
            cmbSumitUnit.DisplayMember = "Name";
            cmbSumitUnit.ValueMember = "Id";

            _ListAll = new DataAccess.tb_SubmitUnit().GetSubmitUnitList();

            //_ListAll = new List<Model.DB.tb_SubmitUnit>()
            //{
            //    new Model.DB.tb_SubmitUnit()
            //    { Id = 1 ,Name = "西北单位1"},
            //    new Model.DB.tb_SubmitUnit()
            //    { Id =2 ,Name = "东北单位1"},
            //    new Model.DB.tb_SubmitUnit()
            //    { Id = 3 ,Name = "东南单位1"},
            //    new Model.DB.tb_SubmitUnit()
            //    { Id = 4 ,Name = "西南单位1"},
            //    new Model.DB.tb_SubmitUnit()
            //    { Id = 5 ,Name = "东西单位1"},
            //    new Model.DB.tb_SubmitUnit()
            //    { Id = 6 ,Name = "南北单位1"},
            //};

            cmbSumitUnit.Items.AddRange(_ListAll.ToArray());
           
            cmbSumitUnit.TextUpdate += CmbSumitUnit_TextUpdate;
            //cmbSumitUnit.DropDownClosed += CmbSumitUnit_DropDownClosed; ;

            if (cmbSumitUnit.Items.Count > 0)
            {
                cmbSumitUnit.SelectedIndex = 0;
            }
        }

        private void CmbSumitUnit_TextUpdate(object sender, EventArgs e)
        {
            try
            {
                if (cmbSumitUnit.DropDownStyle == ComboBoxStyle.DropDownList) return;

                cmbSumitUnit.Items.Clear();
                string stringNew = cmbSumitUnit.Text;
                _ListSearch = new List<Model.DB.tb_SubmitUnit>();

                for (int i = 0; i < _ListAll.Count; i++)
                {
                    if (string.IsNullOrWhiteSpace(stringNew) || _ListAll[i].Name.Contains(stringNew))
                        _ListSearch.Add(_ListAll[i]);
                }

                if (_ListSearch.Count <= 0)
                {
                    _ListSearch.AddRange(_ListAll);
                }
                cmbSumitUnit.Items.AddRange(_ListSearch.ToArray());

                cmbSumitUnit.SelectionStart = cmbSumitUnit.Text.Length;
                //cmbSumitUnit.DroppedDown = true;
                this.Cursor = Cursors.Arrow;
            }
            catch (Exception ex)
            {
                string s1 = string.Empty;
            }
        }

        public int SelectedIndex
        {
            get
            {
                return cmbSumitUnit.SelectedIndex;
            }
            set
            {
                cmbSumitUnit.SelectedIndex = value;
            }
        }
        public object SelectedItem
        {
            get
            {
                return cmbSumitUnit.SelectedItem;
            }
            set
            {
                cmbSumitUnit.SelectedItem = value;
            }
        }
        public string Text
        {
            get
            {
                if (_ListAll.Find(x => x.Name.Equals(cmbSumitUnit.Text)) == null)
                {
                    throw (new Exception("该送检单位名称不存在，请检查输入！！！"));
                }

                return cmbSumitUnit.Text;
            }
            set
            {
                cmbSumitUnit.Text = value;
            }
        }
    }
}
